
#include <iostream>

#include "gtest/gtest.h"
#include "ClientApi.h"
#include <memory>
#include "Log.h"
#include <vector>
#include "ClientApiImp.h"
using namespace CSApi;
static boost::shared_ptr<ClientApi> api;

class Control : public ::testing::Test {
	protected:

		static void SetUpTestCase()	{
			FUNCTION_TRACK
			ASSERT_NO_THROW({
				api = CreateClientApi("127.0.0.1", "5001", "root", "root");
			});
            ASSERT_TRUE(api.get()!=NULL);
		}

		static void TearDownTestCase() {
			ASSERT_NO_THROW({
				api.reset();
			});
		}
};

TEST_F(Control, AddRemoveUser) {
    FUNCTION_TRACK
    ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        int userId = api->AddUser("user", "password");
        ASSERT_FALSE(userId == 0);
        ASSERT_FALSE(api->RemoveUser(userId) == 0);
    });
}

TEST_F(Control, GetUsersList) {
    FUNCTION_TRACK
    ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        vector<User> users = api->GetUsersList();
    });
}

TEST_F(Control, ChangePasswordUser) {
    FUNCTION_TRACK
	ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        int userId = api->AddUser("user", "password");
		ASSERT_FALSE(userId == 0);
        api->ChangeUserPassword("user", "password2");
        boost::shared_ptr<ClientApi> api2 = CreateClientApi("127.0.0.1", "5001", "user", "password2");
        ASSERT_FALSE(userId == 0);
        ASSERT_FALSE(api->RemoveUser(userId) == 0);
    });
}

TEST_F(Control, AddRemoveGroup) {
    FUNCTION_TRACK
    ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        int groupId = api->AddGroup("test_group");
        ASSERT_FALSE(groupId == 0);
        ASSERT_FALSE(api->RemoveGroup(groupId) == 0);
    });
}

TEST_F(Control, GetGroupList) {
    FUNCTION_TRACK
    ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        int groupId = api->AddGroup("test_group2");
        ASSERT_FALSE(groupId == 0);
        vector<Group> groups = api->GetGroupList();
        ASSERT_FALSE(api->RemoveGroup(groupId) == 0);
    });
}

TEST_F(Control, AddRemoveStorage) {
    FUNCTION_TRACK
    ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        int storageId = api->AddStorage("test_storage", "key");
        ASSERT_FALSE(storageId == 0);
        ASSERT_FALSE(api->RemoveStorage(storageId) == 0);
    });
}

TEST_F(Control, ChangeStorageKey) {

    FUNCTION_TRACK
    ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        int storageId = api->AddStorage("test_storage", "key");
        ASSERT_FALSE(storageId == 0);
        ASSERT_FALSE(api->ChangeStorageKey("test_storage", "key2"));
        ASSERT_FALSE(api->RemoveStorage(storageId) == 0);
    });
}

TEST_F(Control, GetStorageList) {
    FUNCTION_TRACK
    ASSERT_TRUE(api.get()!=NULL);
    ASSERT_NO_THROW({
        vector<Storage> storages = api->GetStorageList();
    });
}